// ActionScript file
import component.TreeComboxBoxRender;

import mx.containers.GridItem;
import mx.containers.GridRow;
import mx.controls.Alert;
import mx.controls.DateField;
import mx.controls.TextInput;

/*创建检索条目*/
public var searchGridItemArray:Array;
public var searchValidatorArray:Array;
public var columnsNumber:int;
/*
 * */
public function createSearchPanel(gridTemplate:XML):void
{
	columnsNumber = Number(gridTemplate.@number)*2;
	btnSearch.label = gridTemplate.@okLabel;
	btnReset.label = gridTemplate.@resetLabel;
	searchGridItemArray = new Array();
	searchValidatorArray = new Array();
	for each (var item:XML in gridTemplate.item)
	{
		createSearchItem(item);
	}
	var arrayLength:int = searchGridItemArray.length;
	var beginIndex:int = 0;
	trace(arrayLength);
	while((arrayLength>=columnsNumber)&&arrayLength>0)
	{
		var itemArray:Array = new Array();
		for(var i:int=0; i<columnsNumber; i++)
		{
			itemArray.push(searchGridItemArray[beginIndex+i]);
		}
		createGridRow(itemArray);
		beginIndex = beginIndex + columnsNumber;
		arrayLength = arrayLength - columnsNumber;
	}
	if(arrayLength!=0&&arrayLength<columnsNumber)
	{
		var itemArray:Array=new Array();
		for(var i:int=0; i<arrayLength;i++)
		{
			itemArray.push(searchGridItemArray[beginIndex+i]);
		}
		createGridRow(itemArray);
	}
}
public function createGridRow(rowArray:Array):void
{
	var gridRow:GridRow = new GridRow();
	for(var i:int=0;i<rowArray.length&&i<columnsNumber;i++)
	{
		var gridItem:GridItem = new GridItem();
		gridItem.addChild(rowArray[i]);
		gridRow.addChild(gridItem);
	}
	searchItemGrid.addChild(gridRow);
}
public function createSearchItem(item:XML):void
{
	var itemId:String = item.@name;
	var itemName:String = item.@label;
	var itemType:String = item.@type;
	var model:String = item.@model;//检索类型：equal:等于 gt:大于 lt:小于 like:模糊匹配 空：等于
	var label:Label = new Label();
	label.setStyle("textAlign","right");
	
	label.text = itemName;
	trace("label:"+label.text);
	label.setStyle("textAlign","right");
	searchGridItemArray.push(label);
	if(itemType=="string")
	{
		var textInput:TextInput = new TextInput();
		textInput.id = itemId;
		textInput.width=140;
		if(model!=null&&model!="")
		{
			textInput.styleName=model;
		}
		searchGridItemArray.push(textInput);
		 /* if(validator.length>0)
		{
			var stringValidator:StringValidator = createStringValidator(validator);
			stringValidator.source=textInput;
			stringValidator.property="text";
			searchValidatorArr.push(stringValidator);
		}  */
	}
	else if(itemType=="date")
	{
		var dateField:DateField = new DateField();
		dateField.formatString="YYYY-MM-DD";
		dateField.editable = true;
		if(model!=null&&model!="")
		{
			dateField.styleName=model;
		}
		dateField.id = itemId;
		searchGridItemArray.push(dateField);
	}
	else if(itemType=="comboBox")
	{
		var treeCBR:TreeComboxBoxRender = new TreeComboxBoxRender();
		treeCBR.pageName = item.@pageName;
		treeCBR.id = itemId;
		treeCBR.servicePath = params["gridService"];
		searchGridItemArray.push(treeCBR);
	}
} 
public function setSearchCondition():void
{
	var condition:String = "";
	var flag:int = 0;
	for each(var gridRow:GridRow in searchItemGrid.getChildren())
	{
		for each(var gridItem:GridItem in gridRow.getChildren())
		{
			if(gridItem.getChildAt(0) is TextInput)
			{
				var textInput:TextInput = TextInput(gridItem.getChildAt(0));
				var columnName:String = textInput.id;
				var columnValue:String = textInput.text;
				var model:String ="";
				if(textInput.styleName!=null)
				{
					model = textInput.styleName.toString();
				}
				var itemOfSearch:String = "";
				if(columnValue!=null&&columnValue!="")
				{
					if(model==""||model=="equal")
					{
						itemOfSearch = "='"+columnValue+"'";
					}
					else if(model=="like")
					{
						itemOfSearch = " like '%"+columnValue+"%'";
					}
					else if(model=="gt")
					{
						itemOfSearch = "&gt;'"+columnValue+"'";
					}
					else if(model=="lt")
					{
						itemOfSearch = "&lt;'"+columnValue+"'";
					}
					else
					{
						itemOfSearch = "='"+columnValue+"'";
					}
					if(flag==0)
					{
						condition += columnName+itemOfSearch;
						flag = 1;
					}
					else if(flag==1)
					{
						condition +=" and "+columnName+itemOfSearch;
					}
				}
			}
			else if(gridItem.getChildAt(0) is DateField)
			{
				var dateField:DateField = DateField(gridItem.getChildAt(0));
				var columnName:String = dateField.id;
				var columnValue:String = dateField.text; 
				if(columnValue!=null&&columnValue!="")
				{
					if(flag==0)
					{
						condition +=columnName+"='"+columnValue+"'";
						flag =1;
					}
					else if(flag==1)
					{
						condition +=" and "+columnName+"='"+columnValue+"'";
					}
				}
			}
			else if(gridItem.getChildAt(0) is TreeComboxBoxRender)
			{
				var treeComboBox:TreeComboxBoxRender = TreeComboxBoxRender(gridItem.getChildAt(0));
				var columnName:String = treeComboBox.id;
				if(treeComboBox.selectedItem==null)
				{
					continue;
				}
				var columnValue:String = treeComboBox.selectedItem.@value;
				trace(columnValue);
				if(columnValue!=null&&columnValue!="")
				{
					if(flag==0)
					{
						condition +=columnName+"='"+columnValue+"'";
						flag =1;
					}
					else if(flag==1)
					{
						condition +=" and "+columnName+"='"+columnValue+"'";
					}
				}
			}
		}
	}
	trace(condition);
	params["condition"]=condition;
	search();
}
public function resetSearchItems():void
{
	var growArray:Array = searchItemGrid.getChildren();
	for each(var grow:GridRow in growArray)
	{
		var childsOfGridItemArray:Array = grow.getChildren();
		for each(var gridItem:GridItem in childsOfGridItemArray)
		{
			var obj:Object = gridItem.getChildAt(0);
			if(obj is TextInput)
			{
				obj.text="";
			}
			else if(obj is DateField)
			{
				obj.text="";
			}
			else if(obj is ComboBox)
			{
				obj.selected=false;
			}
		}
	}
}
public function search():void
{
	var paramObj:Object=new Object();
	var tableName:String = params["tableName"];
	var condition:String = params["condition"];
	var inputParam:String = "<data><table>"+tableName+"</table>";
	if(condition=="")
	{
		
	}
	else
	{
		inputParam += "<condition>"+condition+"</condition>";
	}
	inputParam+="<page><start>"+recentPage+"</start><limit>"+numberPerPageComboBox.text+"</limit><orderBy /><orderType/></page>";
	inputParam+="</data>";
	paramObj["in0"]=inputParam;
	var webSrv:WebSrv = new WebSrv(params["gridService"]);
	webSrv.invokeWebSrv("search", paramObj);
	webSrv.onComplete=function(result:String):void
	{
		var resultXml:XML = new XML(result);
		var findResult:String = resultXml.result;
		if(findResult=="ok")
		{
			trace(resultXml.data);
			var data:XML = new XML(resultXml.data.toString());
			dataGrid.dataProvider = data.item;
			resetPageInfo(Number(resultXml.totalRecords));
		}
		else
		{
			mx.controls.Alert.show(findResult);
		}
	}
}
public function research():void
{
	recentPage = 0;
	search();
}